<?php
/**
 * Класс предназначен для загрузки других классов-менеджеров. Реализует паттерн LazyLoad
 * @author shurik2533
 *
 */
class Default_Model_GlobalManager
{
	protected $_managers;
    
	protected static $_instance;
 
    private function __construct() 
    {
    	$this->_managers = array(); 
    }
 
    private function __clone()
    { }
 
    public static function getInstance() 
    {
      if( self::$_instance === NULL ) {
        self::$_instance = new self();
      }
      return self::$_instance;
    }
    
    public function getManager($managerName)
    {
    	if ( !isset($this->_managers[$managerName]) || null === $this->_managers[$managerName] ) {
			$this->_registerManager($managerName, 'Default_Model_Managers_' . $managerName);
		} 
		
    	return $this->_managers[$managerName];
    }
    
    protected function _registerManager($managerName, $managerClass)
    {
    	if ( is_string($managerClass) ) {
			$managerClass = new $managerClass();
		}
		if ( !$managerClass instanceof Default_Model_Managers_Abstract) {
			throw new Exception('Invalid manager provided');
		}
		
		$this->_managers[$managerName] = $managerClass;
		return $this;
    }
    
    /**
     * Возвращает менеджер по указанному имени.
     * @param string $managerName
     * @return Default_Model_Managers_Abstract
     */
    public static function findManager($managerName)
    {
       return self::getInstance()->getManager($managerName); 
    }
}